{% extends 'accounts/base.html' %}

{% load form_extras %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">{{ title }}</h1>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">
                    <i class="fas fa-user-tag"></i> {{ title }}
                </h5>
            </div>
            <div class="card-body">
                <form method="post">
                    {% csrf_token %}
                    
                    <div class="mb-3">
                        <label for="{{ form.name.id_for_label }}" class="form-label">角色名称</label>
                        {{ form.name|add_class:"form-control" }}
                        {% if form.name.errors %}
                            <div class="text-danger">{{ form.name.errors }}</div>
                        {% endif %}
                    </div>
                    
                    <div class="mb-3">
                        <label for="{{ form.description.id_for_label }}" class="form-label">描述</label>
                        {{ form.description|add_class:"form-control" }}
                        {% if form.description.errors %}
                            <div class="text-danger">{{ form.description.errors }}</div>
                        {% endif %}
                    </div>
                    
                    <div class="row">
                        <div class="col-md-12">
                            <div class="mb-3">
                                <label class="form-label">权限</label>
                                <div class="permission-groups">
                                    {% for group_name, group_choices in form.permissions.field.choices %}
                                    <div class="permission-group card mb-3">
                                        <div class="card-header permission-group-header d-flex justify-content-between align-items-center" 
                                             data-bs-toggle="collapse" 
                                             data-bs-target="#permission-group-{{ forloop.counter }}" 
                                             style="cursor: pointer;">
                                            <h6 class="mb-0">{{ group_name }} 权限</h6>
                                            <button type="button" class="btn btn-sm btn-outline-primary select-all-btn" 
                                                    data-group="permission-group-{{ forloop.counter }}">
                                                全选
                                            </button>
                                        </div>
                                        <div id="permission-group-{{ forloop.counter }}" class="collapse show">
                                            <div class="card-body">
                                                <div class="row">
                                                    {% for choice_id, choice_label in group_choices %}
                                                    <div class="col-md-6">
                                                        <div class="form-check">
                                                            <input class="form-check-input" type="checkbox" 
                                                                   name="permissions" 
                                                                   value="{{ choice_id }}"
                                                                   id="id_permissions_{{ choice_id }}"
                                                                   {% if choice_id in form.permissions.value %}checked{% endif %}>
                                                            <label class="form-check-label" for="id_permissions_{{ choice_id }}">
                                                                {{ choice_label }}
                                                            </label>
                                                        </div>
                                                    </div>
                                                    {% endfor %}
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                                {% if form.permissions.errors %}
                                    <div class="text-danger">{{ form.permissions.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-12">
                            <div class="mb-3">
                                <label class="form-label">菜单</label>
                                <div class="row">
                                    {% for choice in form.menus.field.choices %}
                                    <div class="col-md-6">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" 
                                                   name="menus" 
                                                   value="{{ choice.0 }}"
                                                   id="id_menus_{{ choice.0 }}"
                                                   {% if choice.0 in form.menus.value %}checked{% endif %}>
                                            <label class="form-check-label" for="id_menus_{{ choice.0 }}">
                                                {{ choice.1 }}
                                            </label>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                                {% if form.menus.errors %}
                                    <div class="text-danger">{{ form.menus.errors }}</div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-3">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> 保存
                        </button>
                        <a href="{% url 'accounts:role_list' %}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> 返回
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 处理全选按钮
        const selectAllButtons = document.querySelectorAll('.select-all-btn');
        selectAllButtons.forEach(button => {
            button.addEventListener('click', function(e) {
                e.stopPropagation(); // 阻止折叠事件触发
                
                const groupId = this.getAttribute('data-group');
                const group = document.getElementById(groupId);
                const checkboxes = group.querySelectorAll('input[type="checkbox"]');
                
                // 检查是否所有复选框都已选中
                const allChecked = Array.from(checkboxes).every(checkbox => checkbox.checked);
                
                // 如果全部选中，则取消全选；否则全选
                checkboxes.forEach(checkbox => {
                    checkbox.checked = !allChecked;
                });
            });
        });
        
        // 处理权限组标题点击事件，阻止事件冒泡到折叠功能
        const headers = document.querySelectorAll('.permission-group-header');
        headers.forEach(header => {
            header.addEventListener('click', function(e) {
                // 如果点击的是全选按钮，则不触发展开/折叠
                if (e.target.classList.contains('select-all-btn')) {
                    e.stopPropagation();
                }
            });
        });
    });
</script>
{% endblock %}